/*初始化加载
 * pageNav.load(cp,psize);
 * */
/*自定义方法,cp为当前页码,pn为总页数,psize为每页显示条数 datalist为获取到的数据列表*/
var pn;
var Url,CPdata,CPfunction;

function pageNav(Vue){
    this._Vue = Vue;
    this.eventList = [];
}
//根据参数获取列表和所需参数
pageNav.prototype.load = function(cp,psize,url,jsondata,dataList) {
    Url = url;
    CPdata = jsondata;
    CPfunction = dataList;

    var getListData=jsondata;
        getListData.currPage=cp;
        getListData.pageSize=psize;
    this._Vue.$api.method(url,getListData,"GET").then(data=>{
        if(data.rt.status==200 ||data.rt.status==100001||data.rt.status==100002){
            dataList(data.data);
            var newData=data.data;
            
            this.eventList.length = 0;
            if(newData.list != undefined&& newData.list != null && newData.list.length>0){
                //跳转到第cp页
                this.go(cp,psize,url,jsondata,dataList,newData.count,true);
            }else{
                //跳转到第cp页
                this.go(cp,psize,url,jsondata,dataList,newData.count,false);
            }
            //data传给前端
            //dataList?dataList(data.data.list):function(){};
        }else{
            this._Vue.$message(data.data)
        }
    });
}
//cp为当前页码,pn为总页数
pageNav.prototype.nav = function(cp,psize,url,jsondata,dataList,count) {
    //总页数
    pn=Math.ceil(count/psize);
    //只有一页,直接显示1
    if (pn <= 1) {
        this.cp = 1;
        this.pn = 1;
        return "";
        //return this.pHtml2(1);
    }
    if (pn < cp) {
        cp = pn;
    }
    let re = "";
    //第一页
    if (cp <= 1) {
        cp = 1;
    } else {
        //非第一页
        re += this.pHtml(cp-1,psize,url,jsondata,dataList,"上一页");
        //始终显示第一页页码
        re += this.pHtml(1,psize,url,jsondata,dataList,"1");
    }
    //校正页码
    this.cp = cp;
    this.pn = pn;

    //开始页码
    let start = 2;
    let end = (pn < 8) ? pn: 8;
    //是否显示前置省略号,即大于10的开始页码
    if (cp >= 7) {
        re += "<li>...</li>";
        start = cp - 4;
        let e = cp + 4;
        end = (pn < e) ? pn: e;
    }
    for (let i = start; i < cp; i++) {
        re += this.pHtml(i,psize,url,jsondata,dataList);
    }
    re += this.pHtml2(cp);
    for (let i = cp + 1; i <= end; i++) {
        re += this.pHtml(i,psize,url,jsondata,dataList);
    }
    if (end < pn) {
        re += "<li>...</li>";
        //显示最后一页页码,如不需要则去掉下面这一句
        re += this.pHtml(pn,psize,url,jsondata,dataList);
    }
    if (cp < pn) {
        re += this.pHtml(cp+1,psize,url,jsondata,dataList,"下一页");
    }
    return re;
};
//显示非当前页
pageNav.prototype.pHtml = function(pageNo,psize,url,jsondata,dataList,showPageNo) {
    showPageNo = showPageNo || pageNo;
    let that = this;
    that.eventList.push(
        function(){
            let _dom=document.getElementById('xxx'+encodeURI(showPageNo));
            _dom.onclick = function(e){
                that.changecp(e.target.dataset.no,e.target.dataset.size);
            }
        }
    );
    return `<li><a id="xxx${encodeURI(showPageNo)}" data-no=${pageNo} data-size=${psize}>${showPageNo}</a></li>`;
};
//显示当前页
pageNav.prototype.pHtml2 = function(pageNo) {
    return "<li><a class='hover'>" + pageNo + "</a></li>";
};
//输出页码,可根据需要重写此方法
pageNav.prototype.go = function(cp,psize,url,jsondata,dataList,count,flag) {
    if(flag){
        $("#pagesize").html(this.getlist(cp,psize,url,jsondata,dataList,count));
        //给每页显示多少条下拉框绑定事件
        this.eventList.push(
            function(){
                let _dom=document.getElementById('changepage');
                _dom.onclick = function(e){
                    this.changecp(1,e.target.dataset.value);
                }
            }
        );
        //给所有翻页按钮绑定事件
        this.eventList.forEach(fn=>{
            fn();
        })
    }else{
        $("#pagesize").html("");
    }
};
//翻页和切换每页显示条数
pageNav.prototype.changecp = function(cp,psize) {
    this.load(cp,psize,Url,CPdata,CPfunction);
};
pageNav.prototype.getlist = function(cp,psize,url,jsondata,dataList,count) {
    var html=[];
    html[html.length]='<div class="flip">';
    html[html.length]='    <div class="flip-left">';
    html[html.length]='    共<span>'+count+'</span>条记录 每页 <select name="changepage" id="changepage">';
    if(psize==10){
        html[html.length]='    <option value="10" selected = "selected">10</option><option value="20">20</option><option value="50">50</option>';
    }else if(psize==20){
        html[html.length]='    <option value="10">10</option><option value="20" selected = "selected">20</option><option value="50">50</option>';
    }else{
        html[html.length]='    <option value="10">10</option><option value="20">20</option><option value="50" selected = "selected">50</option>';
    }
    html[html.length]='    <select> 条记录';
    html[html.length]='    </div>';
    html[html.length]='    <div class="flip-right">';
    html[html.length]='    <ul>';
    html[html.length]=  this.nav(cp,psize,url,jsondata,dataList,count);
    html[html.length]='    </ul>';
    html[html.length]='    </div>';
    html[html.length]='    </div>';
    return html.join("");
};

export default pageNav